#include "User.h"

User::User(QByteArray *nickC, QHostAddress *addressC, QByteArray *realNameC, bool createUid, QByteArray *id) : QObject()
{
	nick = nickC;
	address = addressC;
	realName = realNameC;
	isBlocked = false;
	lastSeen = QDateTime::currentDateTime().toTime_t();
	sock = new QUdpSocket();
	uID = new QByteArray();
	
	if(createUid)
	{
		createUserID();
	}
	else
	{
		uID->clear();
		uID->append(*id);
	}
}

/**
 * Deconstructor
 */
User::~User()
{
	delete nick;
	delete uID;
	delete sock;
	delete realName;
}

User::User(QByteArray nickC, QString addressC, QByteArray realNameC, bool createUid, QByteArray id) : QObject()
{
	nick = new QByteArray(nickC);
	address = new QHostAddress(addressC);
	realName = new QByteArray(realNameC);
	uID = new QByteArray();
	isBlocked = false;
	lastSeen = QDateTime::currentDateTime().toTime_t();
	sock = new QUdpSocket();	
	
	if(createUid)
	{
		createUserID();
	}
	else
	{
		uID->clear();
		uID->append(id);
	}
}

void User::createUserID()
{
	/* init random seed */
	qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
	uID->append(QByteArray::number((unsigned)(qrand() * 100000)));
	uID->append(QByteArray::number(QTime(0,0,0).secsTo(QTime::currentTime())));
}

void User::updateLastSeen()
{
	lastSeen = QDateTime::currentDateTime().toTime_t();
}

QHostAddress* User::getAddress() { return address; }
QByteArray* User::getNick() { return nick; }
QByteArray* User::getRealName() { return realName; }
QByteArray* User::getID() { return uID; }
uint User::getLastSeen() { return lastSeen; }

/**
 * Set a new nick name
 * @param QString newNick
 */
void User::setNick(QString newNick)
{
	nick->clear();
	nick->append(newNick);
}

/**
 * Set a new real name
 * @param QString newRealName
 */
void User::setRealName(QString newRealName)
{
	realName->clear();
	realName->append(newRealName);
}
